package com.rgsx.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket docket(Environment environment){
        //指定在dev/test环境下使用swagger
//        Profiles profiles = Profiles.of("dev","test");
//        System.out.println(profiles);
//        boolean flag = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.rgsx.controller"))
                .paths(PathSelectors.any()) //定义哪些路径的接口需要生成文档
                .build().globalOperationParameters(this.getParameterList());
    }

    private ApiInfo apiInfo(){
        Contact contact = new Contact("杨柠歌", "", "1578398187@qq.com");
        return new ApiInfo(
                "软工实训 - 杨柠歌的Api",
                "接口文档",
                "v1.0",
                "",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList()
        );
    }

    /**
     * 添加head参数配置
     */
    private List<Parameter> getParameterList() {
        ParameterBuilder clientIdTicket = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        clientIdTicket
                .name("token")
                .description("直接输入token值即可测试，ajax提交时，头的键值是‘token’")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false).build(); //设置false，表示参数 非必填,可传可不传！
        pars.add(clientIdTicket.build());

        return pars;

    }
}

